package com.woyowodaows.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public abstract class BaseFragment extends Fragment {

    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected View layoutView;
    protected int pageSize = 10;
    protected int pageNumber = 0;
    protected int maxPage = 1;
    protected boolean isRefresh = true;
    protected int totalNum = 1;
    protected Context mContext;

    /**
     * rootView是否初始化标志，防止回调函数在rootView为空的时候触发
     */
    private boolean hasCreateView;

    /**
     * 当前Fragment是否处于可见状态标志，防止因ViewPager的缓存机制而导致回调函数的触发
     */
    private boolean isFragmentVisible;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("", "setUserVisibleHint() -> isVisibleToUser: " + isVisibleToUser);
        if (layoutView == null) {
            return;
        }
        hasCreateView = true;
        if (isVisibleToUser) {
            onFragmentVisibleChange(true);
            isFragmentVisible = true;
            return;
        }
        if (isFragmentVisible) {
            onFragmentVisibleChange(false);
            isFragmentVisible = false;
        }
    }

    private void initVariable() {
        hasCreateView = false;
        isFragmentVisible = false;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initVariable();
    }

    /***
     *  onViewCreated是在onCreateView后被触发的事件，前后关系
     *  且onStart运行时间位于onViewCreated之后
     *
     *  onCreateView --> onViewCreated --> onStart  方法的先后运行顺序
     *
     * @param view
     * @param savedInstanceState
     */

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!hasCreateView && getUserVisibleHint()) {
            onFragmentVisibleChange(true);
            isFragmentVisible = true;
        }
    }

    /**
     * 当前fragment可见状态发生变化时会回调该方法
     * 如果当前fragment是第一次加载，等待onCreateView后才会回调该方法，其它情况回调时机跟 {@link #setUserVisibleHint(boolean)}一致
     * 在该回调方法中你可以做一些加载数据操作，甚至是控件的操作，因为配合fragment的view复用机制，你不用担心在对控件操作中会报 null 异常
     *
     * @param isVisible true  不可见 -> 可见
     *                  false 可见  -> 不可见
     */
    protected void onFragmentVisibleChange(boolean isVisible) {
        Log.e("", "onFragmentVisibleChange -> isVisible: " + isVisible);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (layoutView != null) {
            ViewGroup parent = (ViewGroup) layoutView.getParent();
            if (parent != null) {
                parent.removeView(layoutView);
            }
        }else {
            layoutView = inflater.inflate(getLayoutId(), container, false);
        }
        isInit = true;
        mContext = getActivity();
        initView();
        initData();
        initEvent();
        return layoutView;
    }


    public void initData() {

    }

    /**
     * 界面展示
     */
    protected abstract void initView();

    /**
     * 设置Fragment要显示的布局
     *
     * @return 布局的layoutId
     */
    protected abstract int getLayoutId();

    /**
     * 获取设置的布局
     *
     * @return
     */
    protected View getContentView() {
        return layoutView;
    }

    /**
     * 找出对应的控件
     *
     * @param id
     * @param <T>
     * @return
     */
    protected <T extends View> T findViewById(int id) {

        return (T) getContentView().findViewById(id);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void initEvent(){

    }


    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }
}


